<?php

namespace App\Http\Controllers\Admin;

use App\Model\Classify;
use App\Model\News;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $item = News::orderBy('id','desc')->paginate(15);

        return view('admin.news.article-list',['item'=>$item]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.news.article-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
           'articletitle' => 'required'
        ],[
            'articletitle.required'=>'请填写标题'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        if ($request->hasFile('file')){
            $file = $request->file('file');
            $fileName = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$fileName)){
                return $this->error('不支持的图片格式');
            }
            $path = $request->file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            return $this->error('请上传封面图片');
        }

        $m = new News();
        $m->title = $request->input('articletitle');
        $m->ctitle = $request->input('articletitle2');
        $m->classify = $request->input('articlecolumn');
        $m->keywords = $request->input('keywords');
        $m->description = $request->input('abstract');
        $m->content = $request->input('editorValue');
        $m->author = $request->input('sources');
        $m->amount = $request->input('amount');
        $m->imgurl = $path;
        $m->link = $request->input('link');
        $m->top = $request->input('inlineRadioOptions');
        if ($m->save()){
            return $this->success('发表成功');
        }else{
            return $this->error('发表失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item = News::find($id);
        $category = Classify::where('two_level',0)->get();
        $two_level = Classify::where([['two_level','!=',0]])->get();
        $res = array();
        foreach ($category as $value){
            foreach ($two_level as $v) {
                if ($value->id == $v->two_level){
                    $res[] = $v;
                }
            }
        }

        return view('admin.news.article-edit',compact('item','category','res'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'articletitle' => 'required'
        ],[
            'articletitle.required'=>'请填写标题'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        if ($request->hasFile('file')){
            $file = $request->file('file');
            $fileName = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$fileName)){
                return $this->error('不支持的图片格式');
            }
            $path = $request->file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            $path = $request->input('imgurl');
        }

        $m = News::find($id);
        $m->title = $request->input('articletitle');
        $m->ctitle = $request->input('articletitle2');
        $m->classify = $request->input('articlecolumn');
        $m->keywords = $request->input('keywords');
        $m->description = $request->input('abstract');
        $m->content = $request->input('editorValue');
        $m->author = $request->input('sources');
        $m->amount = $request->input('amount');
        $m->imgurl = $path;
        $m->link = $request->input('link');
        $m->top = $request->input('inlineRadioOptions');
        if ($m->save()){
            return $this->success('更改成功');
        }else{
            return $this->error('更改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = News::where('id',$id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->success('删除失败');
        }
    }
    //资讯--批量删除
    public function batch_delete(Request $request)
    {
        $id = $request->input('ids');
        if ($id != null){
            $data = News::whereIn('id',$id)->delete();
            if ($data){
                return $this->success('删除成功');
            }else{
                return $this->error('删除失败');
            }
        }else{
            return $this->error('请选择删除对象');
        }
    }
    //资讯--搜索
    public function search(Request $request)
    {
        $classify = $request->input('classify');
        $keyword = $request->input('keywords');
        if ($classify == 0 && $keyword == null){//如果分类是0 and 搜索关键词为空
            $article = News::orderBy('id','desc')->paginate(15);
        }elseif($classify != 0 && $keyword == null){//如果分类不是0 and 搜索关键词为空
            $article = News::where('classify',$classify)->paginate(15);
        }elseif($classify !=0 && $keyword !== null){//如果分类不是0 and 搜索关键词不为空
            $article = News::where([['classify','=',$classify],['title','like','%'.$keyword.'%']])->paginate(15);
        }else{//如果分类是0或者不是0 and 搜索关键词不为空
            $article = News::where('title','like','%'.$keyword.'%')->paginate(15);
        }
        return view('admin.news.article-search',['item'=>$article]);
    }
}
